package com.it.tydic.estate.module.controller.common;


import com.it.tydic.estate.common.aop.CodeMsg;
import com.it.tydic.estate.common.aop.Result;
import com.it.tydic.estate.module.model.inner.TbDictionary;
import com.it.tydic.estate.module.service.DictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("dictionary")
@Api(tags = "字典维护接口")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    private static final Logger logger=  LoggerFactory.getLogger(UserController.class);

    @PostMapping("/findDicByArray")
    @ApiOperation(value = "根据字典code数组查询字典项", notes = "字典code数组")
    public Result findDicByArray(@RequestBody List<String> arrayCode) { //@PathVariable 获取/get/{id}中id
       //参数验证
       if(arrayCode == null || arrayCode.size() == 0){
           return   Result.error(CodeMsg.EMPTY_PARAM_ERROR);
       }
        Map<String,List<TbDictionary>> dataMap=new HashMap<>();
        List<TbDictionary> dictionaries=dictionaryService.findDicByArray(arrayCode);
        for(TbDictionary dictionary : dictionaries){
            if(!dataMap.containsKey(dictionary.getGroupCode())){
                List<TbDictionary> tbDictionaries=new ArrayList<>();
                tbDictionaries.add(dictionary);
                dataMap.put(dictionary.getGroupCode(),tbDictionaries);
            }else{
                dataMap.get(dictionary.getGroupCode()).add(dictionary);
            }
        }
        return   Result.success(dataMap);
    }


}
